package com.greedy.controller;

import com.greedy.bean.dto.UserDTO;
import com.greedy.bean.pojo.Blog;
import com.greedy.constants.SystemConstants;
import com.greedy.interceptor.UserHolder;
import com.greedy.service.BlogService;
import com.greedy.utils.Result;
import org.n3r.eql.Eql;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Yanxy
 * @Date: Created in:2022-12-24
 * Description: 博客
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private BlogService blogService;

    /**
     * 保存博客
     * @param blog 实体
     * @return 实体
     */
    @PostMapping
    public Result saveBlog(@RequestBody Blog blog) {
        return blogService.saveBlog(blog);
    }

    /**
     * 点赞功能
     * @param id
     * @return
     */
    @PutMapping("/like/{id}")
    public Result likeBlog(@PathVariable("id") Long id) {
        blogService.likeBlog(id);
        return Result.ok();
    }

    /**
     * 获取本人发布的博客
     * @param current
     * @return
     */
    @GetMapping("/of/me")
    public Result queryMyBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        List<Blog> blogs = blogService.queryMyBlog(current);
        return Result.ok(blogs);
    }

    /**
     * 获取热点博客
     * @param current 页码
     * @return 热点博客
     */
    @GetMapping("/hot")
    public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        List<Blog> blogs = blogService.queryHotBlog(current);
        return Result.ok(blogs);
    }

    @GetMapping("/{id}")
    public Result queryBlogById(@PathVariable("id") Long id) {
        return blogService.queryBlogById(id);
    }

    /**
     * 博客详情-点赞前五名
     * @param id
     * @return
     */
    @GetMapping("/likes/{id}")
    public Result queryBlogLikes(@PathVariable("id") Long id) {
        return Result.ok(blogService.queryBlogLikes(id));
    }

    @GetMapping("/of/user")
    public Result queryBlogByUserId(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam("id") Long id) {
        // 根据用户查询
        List<Blog> blogs = blogService.queryBlogByUserId(current, id);
        return Result.ok(blogs);
    }

    @GetMapping("/of/follow")
    public Result queryBlogOfFollow(@RequestParam("lastId") Long max,
                                    @RequestParam(value = "offset", defaultValue = "0") Integer offset){
        return blogService.queryBlogOfFollow(max, offset);
    }

}
